home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / AEWIN100.ARJ / VIDEO.CC < prev    next >
C/C++ Source or Header  |  1991-11-02  |  7KB  |  320 lines

  1. /**********************************************************************
  2.  *  
  3.  *  NAME:           video.cpp
  4.  *  
  5.  *  DESCRIPTION:    video functions for Turbo C video library
  6.  *  
  7.  *  copyright (c) 1990 J. Alan Eldridge
  8.  * 
  9.  *  M O D I F I C A T I O N   H I S T O R Y
  10.  *
  11.  *  when        who                 what
  12.  *  -------------------------------------------------------------------
  13.  *  08/05/90    J. Alan Eldridge    created
  14.  *
  15.  *  08/12/90    JAE                 removed some error checking,
  16.  *                                  made some functions void
  17.  *  
  18.  *  12/21/90    JAE                 un-object-oriented it, because
  19.  *                                  it just didn't seem to fit
  20.  *
  21.  *********************************************************************/
  22.  
  23. #include    "aedef.h"
  24. #include    "vidchr.h"
  25. #include    "video.h"
  26.  
  27. #if defined(__TURBOC__)
  28. #include    "envutils.h"
  29. #endif
  30.  
  31. //  static storage for video driver
  32.  
  33. static  struct text_info    tInfo;  //  so we can put the world back
  34.  
  35. static  uchar   mode;           //  mode we're in
  36. static  uchar   nRows,          //  size of physical display
  37.                 nCols;
  38. static  uchar   cRow,           //  current cursor position
  39.                 cCol;
  40. static  uchar   fOpen,          //  device has been opened
  41.                 fColor,         //  device has color support
  42.                 fSaved;         //  image has been saved
  43. static  vidchr  *pSvImg;        //  ptr to saved image
  44.  
  45. static  uchar   curscnt,        //  count for show/hide cursor
  46.                 curstype;       //  type cursor would be if shown
  47.  
  48. //  global for default video attribute
  49.  
  50. uchar   vid_defaultatt = vid_mkatt(LIGHTGRAY,BLACK);
  51.  
  52. //  globals to avoid function call overhead
  53.  
  54. int     vid_ROWS, vid_COLS, vid_ISCOLOR;
  55.  
  56. //  flags handling
  57.  
  58. static void
  59. zflags()    
  60. {
  61.     vid_ROWS = vid_COLS = 0;
  62.     fOpen = fColor = fSaved = vid_ISCOLOR = FALSE;
  63.  
  64. int
  65. vid_isopen()
  66. {
  67.     return fOpen;
  68. }
  69.  
  70. int
  71. vid_iscolor()
  72. {
  73.     return fColor;
  74. }
  75.  
  76. //  write (up to) one row of screen memory
  77.  
  78. int
  79. vid_write(
  80.     uchar   atRow,
  81.     uchar   atCol,
  82.     vidchr   *pBuf,
  83.     uchar   nChrs)
  84. {
  85.     ++atRow, ++atCol;
  86.     return puttext(atCol, atRow, atCol + nChrs - 1, atRow, pBuf) ? OK : ERR;
  87. }
  88.  
  89. //  read (up to) one row of screen memory
  90.  
  91. int
  92. vid_read(
  93.     uchar   atRow,
  94.     uchar   atCol,
  95.     vidchr   *pBuf,
  96.     uchar   nChrs)
  97. {
  98.     ++atRow, ++atCol;
  99.     return gettext(atCol, atRow, atCol + nChrs - 1, atRow, pBuf) ? OK : ERR;
  100. }
  101.  
  102. //  set physical cursor position
  103.  
  104. int
  105. vid_setcpos(
  106.     uchar   atRow,
  107.     uchar   atCol)
  108. {
  109.     if (atRow < nRows && atCol < nCols) {
  110.         cCol = atCol++;
  111.         cRow = atRow++;
  112.         gotoxy(atCol, atRow);
  113.         return OK;
  114.     } else
  115.         return ERR;
  116. }
  117.  
  118. //  get physical cursor position
  119.  
  120. void
  121. vid_getcpos(
  122.     uchar   *pRC)
  123. {
  124.     pRC[0] = cRow;
  125.     pRC[1] = cCol;
  126. }
  127.  
  128. //  clear the screen
  129.  
  130. void
  131. vid_clear(
  132.     uchar   att)
  133. {
  134.     textattr(att);
  135.     clrscr();
  136. }
  137.  
  138. //  initialize the video mode & try to save the image
  139.  
  140. void
  141. vid_open(
  142.     int     newMode,
  143.     char    *evar)
  144. {
  145.     struct text_info    newInfo;
  146.     
  147.     if (fOpen) return;
  148.     
  149.     zflags();
  150.     newMode = vid_envmode(newMode, evar);
  151.     gettextinfo(&tInfo);
  152.     if (newMode != LASTMODE) {
  153.         textmode(newMode);
  154.         gettextinfo(&newInfo);
  155.     } else
  156.         newInfo = tInfo;
  157.     mode = newInfo.currmode;
  158.     vid_ROWS = nRows = newInfo.screenheight;
  159.     vid_COLS = nCols = newInfo.screenwidth;
  160.     if (newInfo.currmode != MONO && newInfo.currmode != BW80 &&
  161.         newInfo.currmode != BW40)
  162.         vid_ISCOLOR = fColor = TRUE;
  163.     pSvImg = new vidchr [ tInfo.screenwidth * tInfo.screenheight ];
  164.     if (pSvImg) {
  165.         gettext(1, 1, tInfo.screenwidth, tInfo.screenheight, pSvImg);
  166.         fSaved = TRUE;
  167.     }
  168.     curscnt = 0;
  169.     _setcursortype(curstype = _NORMALCURSOR);
  170.     fOpen = TRUE;
  171.     atexit(vid_close);
  172. }
  173.  
  174. //  restore saved video mode & image
  175.  
  176. void
  177. vid_close()
  178. {
  179.     if (fOpen) {
  180.         textmode(tInfo.currmode);
  181.         textattr(tInfo.normattr);
  182.         if (fSaved) {
  183.             puttext(1, 1, tInfo.screenwidth, tInfo.screenheight, pSvImg);
  184.             delete pSvImg;
  185.             gotoxy(tInfo.curx, tInfo.cury);
  186.         } else {
  187.             clrscr();
  188.             gotoxy(1, 1);
  189.         }
  190.     }
  191.     _setcursortype(_NORMALCURSOR);
  192.     zflags();
  193. }
  194.         
  195. //  parse environment variable & return desired video mode
  196.  
  197. int
  198. vid_envmode(
  199.     int     mode,
  200.     char    *varName)
  201. {
  202. #if defined(__TURBOC__)
  203.     static struct {
  204.         char    *name;
  205.         int     mode;
  206.     } eVals[] = {
  207.         "MONO",     MONO,
  208.         "CO40",     C40,
  209.         "BW40",     BW40,
  210.         "CO80",     C80,
  211.         "BW80",     BW80,
  212.         "C4350",    C4350,
  213.         "",         LASTMODE
  214.     };
  215.     
  216.     int     nVals;    
  217.     char    *vals[2];
  218.     
  219.     if (!varName || !varName[0])
  220.         return mode;
  221.         
  222.     if (nVals = envParse(varName, ";, \t", vals, 2, 0)) {
  223.         for (int n = 0; eVals[n].name[0]; n++) {
  224.             if (!stricmp(vals[0], eVals[n].name) 
  225.                 || (nVals > 1 && !stricmp(vals[1], eVals[n].name))) {
  226.                 mode = eVals[n].mode;
  227.                 break;
  228.             }
  229.         }
  230.         if (!stricmp(vals[0], "BIOS") || (nVals > 1 && !stricmp(vals[1], "BIOS")))
  231.             directvideo = 0;
  232.     }
  233.     return mode;
  234. #else
  235.     return LASTMODE;
  236. #endif
  237. }
  238.  
  239. int
  240. vid_rows()
  241. {
  242.     return nRows;
  243. }
  244.  
  245. int
  246. vid_cols()
  247. {
  248.     return nCols;
  249. }
  250.  
  251. //  high level cursor handling
  252.  
  253. void
  254. vid_hidecursor(int newcnt)
  255. {
  256.     int musthide = !curscnt;
  257.     
  258.     if (newcnt)
  259.         curscnt = newcnt;
  260.     else
  261.         curscnt--;
  262.         
  263.     if (musthide)
  264.         _setcursortype(_NOCURSOR);
  265. }
  266.  
  267. void
  268. vid_showcursor()
  269. {
  270.     if (curscnt < 0)
  271.         ++curscnt;
  272.     if (curscnt == 0)
  273.         _setcursortype(curstype);
  274. }
  275.  
  276. void
  277. vid_forcecursor(int &oldcnt)
  278. {
  279.     oldcnt = curscnt;
  280.     curscnt = 0;
  281.     _setcursortype(curstype);
  282. }
  283.     
  284. int
  285. vid_cursortype(int type)
  286. {
  287.     int oldtype = curstype;
  288.     
  289.     curstype = type;
  290.     if (curscnt == 0)
  291.         _setcursortype(curstype);
  292.     return oldtype;
  293. }
  294.  
  295. int
  296. vid_cursortype()
  297. {
  298.     return curstype;
  299. }
  300.  
  301. #if NOT_NEEDED
  302.  
  303. //  this class exists just to make sure the screen 
  304. //  gets put back when the program exits: the destructor
  305. //  for the static variable Video calls vid_close()
  306.  
  307. class video {
  308. public:
  309.     ~video()
  310.         { vid_close(); }
  311. };
  312.  
  313. static video Video;
  314.  
  315. #endif
  316.  
  317.  
  318.  
  319.